package beautiful.butterfly.drds.data_exchange.taskgroup.runner;


import beautiful.butterfly.drds.data_exchange.enums.State;
import beautiful.butterfly.drds.data_exchange.exception.DataExchangeException;
import beautiful.butterfly.drds.data_exchange.taskgroup.TaskGroupContainer;
import beautiful.butterfly.drds.data_exchange.util.FrameworkErrorCode;

public class TaskGroupContainerRunner implements Runnable
{

    private TaskGroupContainer taskGroupContainer;

    private State state;

    public TaskGroupContainerRunner(TaskGroupContainer taskGroup)
    {
        this.taskGroupContainer = taskGroup;
        this.state = State.succeeded;
    }


    public void run()
    {
        try
        {
            Thread.currentThread().setName(
                    String.format("taskGroup-%d", this.taskGroupContainer.getTaskGroupId()));
            this.taskGroupContainer.start();
            this.state = State.succeeded;
        } catch (Throwable e)
        {
            this.state = State.failed;
            throw DataExchangeException.asDataExchangeException(
                    FrameworkErrorCode.RUNTIME_ERROR, e);
        }
    }

    public TaskGroupContainer getTaskGroupContainer()
    {
        return taskGroupContainer;
    }

    public State getState()
    {
        return state;
    }

    public void setState(State state)
    {
        this.state = state;
    }
}
